<?php


namespace app\common\model\mysql;


class Goods extends BaseModel
{

    //如果有title 就执行这个方法
    public function searchTitleAttr($query, $value)
    {
        $query->where('title', 'like', '%' . $value . '%');
    }

    //如果有time 就执行这个方法
    public function searchCreateTimeAttr($query, $value)
    {
        $query->whereBetweenTime('create_time', $value[0], $value[1]);
    }


    //获取分类数据 并分页 搜索
    public function getLists($likeKeys, $data, $num = 10)
    {

        $order = [
            "listorder" => "desc",
            "id" => "desc"
        ];

        //withSearch搜索器
        if (!empty($likeKeys)) {
            $res = $this->withSearch($likeKeys, $data);
        } else {
            $res = $this;
        }

        $result = $res->where("status", "<>", 99)
            ->order($order)
            ->paginate($num);
        //echo $this->getLastSql();exit();
        return $result;
    }


    //获取首页大图
    public function getNormalGoodsByCondition($where, $field = true, $limit = 5)
    {
        $order = [
            "listorder" => "desc",
            "id" => "desc"
        ];

        $where["status"] = config("status.success");
        $result = $this->where($where)
            ->order($order)
            ->field($field)
            ->limit($limit)
            ->select();

        return $result;
    }

    //图片获取器
    public function getImageAttr($value)
    {
        //获取当前域名 拼接上图片地址
        return request()->domain() . $value;
    }

    //商品详情页图片获取器
    public function getCarouselImageAttr($value)
    {
        //变成数组
        if (!empty($value)) {
            $value = explode(",", $value);
            $value = array_map(function ($v) {
                return request()->domain().$v;
            },$value);
        }
        return  $value;
    }


    // 根据分类id 获取前10个商品表 sku id 和商品数据
    public function getNormalGoodsFindInSetCategoryId($categoryId, $field = true, $limit = 10)
    {
        $order = [
            "listorder" => "desc",
            "id" => "desc"
        ];
        //搜索category_path_id有没有$categoryId
        $result = $this->whereFindInSet("category_path_id", $categoryId)
            ->where("status", "=", config("status.success"))
            ->order($order)
            ->field($field)
            ->limit($limit)
            ->select();

        // echo $this->getLastSql();exit();

        return $result;

    }


    //商品列表页
    public function getNormalLists($data, $num = 10, $field = true, $order)
    {

        $res = $this;
        if (isset($data['category_path_id'])) {
            $res = $this->whereFindInSet("category_path_id", $data['category_path_id']);
        }
        $list = $res->where("status", "=", config("status.success"))
            ->order($order)
            ->field($field)
            ->paginate($num);
        return $list;


    }


    //前端搜索页面
    public function getSearchLists($likeKeys, $data, $num = 10, $order, $field)
    {
        //withSearch搜索器
        if (!empty($likeKeys)) {
            $res = $this->withSearch($likeKeys, $data);
        } else {
            $res = $this;
        }

        $result = $res->where("status", "<>", 99)
            ->order($order)
            ->field($field)
            ->paginate($num);

        return $result;
    }

}